package com.railwayservice.serviceprovider.web;

import com.railwayservice.application.config.AppConfig;
import com.railwayservice.application.interceptor.Authorize;
import com.railwayservice.application.vo.ResultMessage;
import com.railwayservice.serviceprovider.entity.ServiceType;
import com.railwayservice.serviceprovider.service.ServiceTypeService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author lixs
 * @date 2017年2月17日
 * @describe 服务管理控制器。
 */

@Controller
@RequestMapping(value = "/serviceType", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "服务管理控制器", description = "服务管理模块的相关操作")
@Authorize(type = AppConfig.AUTHORITY_ADMIN, value = {"WEB_SERVICE_SERVICE_MANAGE"})
public class ServiceTypeController {
    private final Logger logger = LoggerFactory.getLogger(ServiceTypeController.class);
    private ServiceTypeService serviceTypeService;

    @Autowired
    public void setServiceTypeService(ServiceTypeService serviceTypeService) {
        this.serviceTypeService = serviceTypeService;
    }

    /**
     * 服务添加
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage addService(ServiceType serviceType) {
        logger.info("服务管理层->添加服务->服务名称：" + serviceType.getName());

        serviceTypeService.addServiceType(serviceType);
        return ResultMessage.newSuccess("添加服务完成！");

    }

    /**
     * 服务删除
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage deleteService(String typeId) {
        logger.info("服务管理层->服务删除->服务id：" + typeId);

        serviceTypeService.deleteServiceType(typeId);
        return ResultMessage.newSuccess("服务删除完成！");

    }

    /**
     * 服务更新
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage updateService(ServiceType serviceType) {
        logger.info("服务管理层->服务更新->服务名称：" + serviceType.getName());

        serviceTypeService.updateServiceType(serviceType);
        return ResultMessage.newSuccess("服务更新完成！");

    }

    /**
     * 服务查询(根据id查询)
     */

    @ResponseBody
    @RequestMapping(value = "/find", method = {RequestMethod.GET,RequestMethod.POST})
    @Authorize(type = AppConfig.AUTHORITY_NONE)
    public ResultMessage findService(@RequestParam String id) {
        logger.info("服务管理层->服务查询->服务id：" + id);

        ServiceType serviceType = serviceTypeService.findServiceType(id);
        return ResultMessage.newSuccess().setData(serviceType);

    }

    /**
     * 服务查询(根据name查询)
     */

    @ResponseBody
    @RequestMapping(value = "/findServiceByName", method = {RequestMethod.GET,RequestMethod.POST})
    @Authorize(type = AppConfig.AUTHORITY_NONE)
    public ResultMessage findServiceByName(@RequestParam String name) {
        logger.info("服务管理层->服务查询->服务名称：" + name);

        List<ServiceType> serviceType = serviceTypeService.findByName(name);
        return ResultMessage.newSuccess().setData(serviceType);

    }

    /**
     * 服务查询(查询所有)
     */
    @ResponseBody
    @RequestMapping(value = "/findAll", method = {RequestMethod.GET,RequestMethod.POST})
    @Authorize(type = AppConfig.AUTHORITY_NONE)
    public ResultMessage findAllService(String order) {
        logger.info("服务管理层->服务查询");
        List<ServiceType> serviceType = serviceTypeService.findAllServiceType(order);
        return ResultMessage.newSuccess().setData(serviceType);
    }
}


